<template>
  <el-dialog
    :title="dialoginTitle"
    :visible="isShow"
    width="30%"
    @close="handleClose"
    @open="getSimpleUserList"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" type="text" placeholder="1-10个字符串" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" type="text" placeholder="1-10个字符串" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择" style="width:80%">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" :rows="3" type="textarea" placeholder="1-300个字符串" style="width:80%" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="clickSubmit">确 定</el-button>
    </template>

  </el-dialog>
</template>

<script>
import { reqGetSimpleUserList } from '@/api/user'
import { reqAddDepartment, reqEditDepart, reqGetDepartDetail } from '@/api/departments'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false

    },
    nodeData: {
      type: Object,
      required: true
    },
    deptsList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validateName = (rule, value, callback) => {
      console.log(value, this.nodeData.id, this.deptsList)
      let arr = []
      if (this.form.id) {
        if (value === this.nodeData.name) {
          callback()
          return
        }
        arr = this.deptsList.filter(item => item.pid === this.nodeData.pid)
      } else {
        arr = this.deptsList.filter(item => item.pid === this.nodeData.id)
        console.log(arr, 5555)
      }

      const isRepeat = arr.some(item => item.name === value)
      console.log(isRepeat)
      isRepeat ? callback(new Error('同级部门已经出现该部门名称了')) : callback()
    }
    const validateCode = (rule, value, callback) => {
      if (this.form.id && value === this.nodeData.code) {
        callback()
        return
      }
      const isRepeat = this.deptsList.some(item => item.code === value)
      isRepeat ? callback(new Error('已经出现该编码了')) : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门名称只能是1-10位', trigger: ['blur', 'change'] },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门编码只能是1-10位', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍只能是1-300位', trigger: ['blur', 'change'] }
        ]
      },
      userList: []
      // dialoginTitle: '大标题'
    }
  },
  computed: {
    dialoginTitle() {
      return this.form.id ? '编辑部门' : '添加子部门'
    }
  },
  methods: {
    handleClose() {
      this.$emit('closeDialog', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    async  getSimpleUserList() {
      const { data } = await reqGetSimpleUserList()
      console.log(data, 6666)
      this.userList = data
    },
    // 点击提交1.表单校验2.发请求3.提示4.关闭对话框，5.渲染
    clickSubmit() {
      this.$refs.form.validate(async(flag) => {
        if (!flag) return
        let res
        // 判断是点击编辑时发送的请求
        if (this.form.id) {
          res = await reqEditDepart(this.form)
          console.log(res, 130130)
          this.$message.success('编辑部门成功')
          // 注意这里部门编码校验有问题，部门名称也有问题
        } else {
          res = await reqAddDepartment({
            ...this.form,
            pid: this.nodeData.id

          })
          console.log(res, 115)
          this.$message.success(res.message)
        }

        this.handleClose()
        this.$emit('add-depts')
      })
    },
    // 获取部门详情
    async  getDepartDetail() {
      const { data } = await reqGetDepartDetail(this.nodeData.id)
      console.log(data, 128128)
      this.form = data
    }

  }
}
</script>

<style>

</style>
